﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using G.WeiBo.WebAPI.Domain;
using Microsoft.Phone.Controls;
using G.Controls;

namespace G.WeiBo.LoacalControls
{
    public abstract class BaseMsgControl : Control
    {
        public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(Message), typeof(BaseMsgControl), new PropertyMetadata(null, (s, e) =>
        {
            BaseMsgControl bc = s as BaseMsgControl;
            bc.MessageChanged(s, e);
        }));

        public Message Message
        {
            get
            {
                return (Message)this.GetValue(MessageProperty);
            }
            set
            {
                this.SetValue(MessageProperty, value);
            }
        }

        protected virtual void MessageChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { }
    }


    public class MessageControl : BaseMsgControl
    {
        public static readonly DependencyProperty MessageChildControlProperty = DependencyProperty.Register("MessageChildControl", typeof(MessageChildControl), typeof(MessageControl), null);

        public MessageChildControl MessageChildControl
        {
            get
            {
                return (MessageChildControl)this.GetValue(MessageChildControlProperty);
            }
            set
            {
                this.SetValue(MessageChildControlProperty, value);
            }
        }
        protected override void MessageChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            base.MessageChanged(sender, e);
            Message msg = e.NewValue as Message;
            if (msg != null && msg.Retweeted != null)
            {
                if (this.MessageChildControl == null)
                    this.MessageChildControl = new MessageChildControl { Message = msg.Retweeted };
                else
                {
                    
                    this.MessageChildControl.Message = msg.Retweeted;
                    this.MessageChildControl.Visibility = System.Windows.Visibility.Visible;
                }
            }
            else
            {
                if (this.MessageChildControl != null)
                    this.MessageChildControl.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
        public MessageControl()
        {
            this.DefaultStyleKey = typeof(MessageControl);
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            TextButton btnNickName = this.GetTemplateChild("btnNickName") as TextButton;
            if (btnNickName != null)
            {
                btnNickName.Click += (s, e) =>
                {
                    G.Navigate.NavigationService.Navigate("/View/UserInfoPage.xaml", this.Message.User);
                };
            }

            ImageButton headImg = this.GetTemplateChild("headImg") as ImageButton;
            if (headImg != null)
            {
                headImg.Click += (s, e) =>
                {
                    G.Navigate.NavigationService.Navigate("/View/UserInfoPage.xaml", this.Message.User);
                };
            }


        }

    }
}
